<template>
    <div id="onlinePPT">
        <div class="title-wrap">
            <div class="title-text">
            </div>
            <div class="title-btns">
                <img src="../../assets/img/mini-w.png" @click="$electron.ipcRenderer.send('resource-window-minimize',{metaId:'onlinePPT'})" alt="" />
                <img src="../../assets/img/close-w.png" @click="$electron.ipcRenderer.send('close-resource-window',{metaId:'onlinePPT'})" alt="" />
            </div>
        </div>
        <div class="pptPreview">
            <iframe
                :src="PPtPreViewUrl"
                id="iframeOnlinePPT"
                width="100%"
                height="100%"
                scrolling="no"
                frameborder="0"
            >
            </iframe>
        </div>
    </div>
</template>

<script>
import {queryResolveAddr} from '../../api/lesson.js'
import { ShowPpt } from '../../../content/utils/ppt'
import { SqlLib } from '../../../../../static/lib/sqlite/SqlLib'
import { SqlLibUser } from '../../../../../static/lib/sqlite/service/SqlLibUser'
import { SqlLibBook } from '../../../../../static/lib/sqlite/service/SqlLibBook'
export default {
    name:'onlinePPT',
    components: {},
    data() {
    return {
        PPtPreViewUrl:'',
        pptId:'',
        lessonId:'',
        showPptObj: new ShowPpt(),
        pptType:'',
    };
    },
    computed: {},
    watch: {},
    methods: {
    
    },
    created() {
    
    },
    mounted() {
        console.log('params' , this.$route.params);
        this.pptId = this.$route.params.onlinePPtId
        this.lessonId = this.$route.params.lessonId
        this.pptType = this.$route.params.pptType
        console.log('this.pptType' ,this.pptType);
        this.queryPPtUrl()
    },
    methods:{
        queryPPtUrl(){
            let sqlLib = SqlLib.getInstance(window.$DBDIR)
            let user = SqlLibUser.queryCurrentUser(sqlLib)
            let userResource = SqlLibBook.queryDefaultPpt(sqlLib, this.lessonId)
            let bsr = userResource.resourceDescription
            let fileName = JSON.parse(bsr).downloadInfo.FileName
            queryResolveAddr({
                id:this.pptId,
                type:'1'
            }).then(res=>{
                console.log(res);
                const path = require('path')
                if (res.code == '0' && res.returnValue.resolveAddr !== null){
                    this.PPtPreViewUrl = res.returnValue.resolveAddr
                }else{
                    this.$message.warning({
                        message:'在线模式打开失败，即将切换为本地模式',
                        type:'warning',
                        duration:5000,
                    })
                    setTimeout(() => {
                        if (this.pptType == "selfPpt") {
                            this.showPptObj.openPpt(path.resolve(window.$DATADIR, 'resc', 'user', 'ppt', user.id, fileName))
                        }else{
                            const path = require('path')
                            var respath = path.resolve(window.$DATADIR, 'resc', 'courseware',  userResource.resourceId + '.pptx')
                            if(fs.existsSync(respath)){
                                this.showPptObj.openPpt(respath)
                            }else{
                                this.showPptObj.openPpt(path.resolve(window.$DATADIR, 'resc', 'courseware',  userResource.resourceId + '.ppt'))
                            }
                        }
                        this.$electron.ipcRenderer.send('close-resource-window',{metaId:'onlinePPT'})  
                    }, 3500);
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
#onlinePPT{
    overflow: hidden;
    .title-wrap {
        // position: fixed;
        // top: 0;
        // left: 0;
        width: 100%;
        height: 36px;
        background-color: rgb(27, 27, 27);
        -webkit-app-region: drag;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 30px;
        box-sizing: border-box;
        z-index: 98;
        .title-btns {
          -webkit-app-region: no-drag;
            img {
                width: 14px;
                height: 14px;
                margin-left: 10px;
                cursor: pointer;
            }
        }
    }
    .pptPreview{
        position: absolute;
        bottom: 0;
        width: 100vw;
        height: calc(100vh - 36px);
        overflow: hidden;
    }
}
</style>